package com.yangzhe.algorithm.c009;

public class ListReverse {
    public static void main(String[] args) {
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(5);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = null;

        printList(reverseList(node1));
    }

    public static class Node {
        public int value;

        public Node next;

        public Node(int i) {
            value = i;
        }
    }


    public static Node reverseList(Node head) {
        Node pre = null;
        Node current = head;
        Node next = null;
        while (current != null) {
            next = current.next;
            current.next = pre;
            pre = current;
            current = next;
        }

        return pre;
    }

    public static void printList(Node head) {
        while (head != null) {
            System.out.print(head.value + ",");
            head = head.next;
        }
    }

}
